﻿using Direct.IDAL.User.Jurisdiction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectSell.Model.User;
using Direct.IDAL;
using DirectSell.Common.Session;
using DirectSell.Model;

namespace DirectSell.BLL.User.Jurisdiction
{
    /// <summary>
    /// 用户登录服务层
    /// </summary>
    public class LoginService:UserService
    {
        #region 日志记录--郭文昊--2018年10月28日20:11:16
        /// <summary>
        /// 日志记录文件
        /// </summary>
        protected override string LogFile => "用户登录";
        #endregion

        #region 构造函数--郭文昊--2018年10月28日20:08:26
        /// <summary>
        /// 用户登录数据访问接口
        /// </summary>
        protected ILoginRepository loginRespository { get; set; }
        public LoginService(ILoginRepository loginRespository)
        {
            this.loginRespository = loginRespository;
        }
        #endregion

        #region 会员登录操作--郭文昊--2018年10月28日20:13:53
        /// <summary>
        /// 会员登录操作
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        protected override async Task<UserRespCode> OperationAsync<T>(T param)
        {

            //1.Model验证是否为空
            //2.判断用户名和密码是否正确
            //3.写入Session
            var model = CheckModel(param);
            if (model == null)
            {
                return UserRespCode.传入参数不正确;
            }
            if(await loginRespository.CheckInformation(model))
            {
                var userinfo = await loginRespository.GetInfo(model);
                if (userinfo.UserStatus == 2)
                {
                    return UserRespCode.该用户已被冻结请联系管理员;
                }
                //存session
                SessionModel session = new SessionModel()
                {
                    UserId = userinfo.UserId,
                    UserCode = userinfo.UserCode,
                    RefereeCode = userinfo.RefereeCode,
                    ServiceCentreCode = userinfo.ServiceCentreCode,
                    UserName = userinfo.UserName,
                    UserLevel = userinfo.UserLevel
                };
                SessionInfo.SetSession(session);
                return UserRespCode.成功;
            }
            

            return UserRespCode.验证失败;
        }
        #endregion

        #region Model验证--郭文昊--2018年8月19日13:10:35
        /// <summary>
        /// Model验证
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param"></param>
        /// <returns></returns>
        protected  UserLoginInfo CheckModel<T>(T param)
        {
            return param as UserLoginInfo;
        }
        #endregion

    }
}
